package serialize.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

public class FastJsonDemo {
    
    @Test
    //FastJson在进行序列化的时候，是通过get方法取的属性值
    void test1() {
        Student student = new Student();
        student.setName("zhansan");
        System.out.println(JSON.toJSONString(student));
    }

    @Test
    //FastJson在进行序列化的时候，不要用is开头的属性，因为在json化会被截断
    void test2() {
        Student2 student2 = new Student2(true, "hh");
        System.out.println(JSON.toJSONString(student2));
    }

    @Test
    //map -> json 会丢失value为null的值
    void test3() {
        Map<String,String> map = new HashMap<String, String>();
        map.put("aaa",null);
        map.put("bbb",null);
        map.put("ccc",null);
        System.out.println(map);
        //String s = JSON.toJSONString(map);
        String s = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
        System.out.println(s);
        Map<String, String> stringMap = JSON.parseObject(s, new TypeReference<Map<String, String>>() {
        });
        System.out.println(stringMap);
    }
    
}
